﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vux.SVCore.Objects;
using Vux.SVCore.Client.Network;
using System.Threading;
using Vux.SVCore.Network;
using System.Net;
using System.Net.Sockets;
using Vux.Helper.Imaging;
using System.Drawing;
using Vux.SVCore.Delegate;

namespace Vux.SVCore.Client
{
    public class SVCoreClient
    {
        private SocketClient m_socket_handle;
        private byte[] m_img_data;
        private string m_str_tinMat;

        public event MsgNotifyHandler MsgNotify;

        protected virtual void OnMsgNotify(object sender, string msg)
        {
            if (MsgNotify != null)
            {
                MsgNotify(sender, msg);
            }
        }

        public string TinMat
        {
            get { return m_str_tinMat; }
            set { m_str_tinMat = value; }
        }

        public byte[] ImageData
        {
            get { return m_img_data; }
            set { m_img_data = value; }
        }


        public SVCoreClient()
        {
        }

        // event
        public event AuthCompleteHandler AuthComplete;
        public event SVCoreDisconnectedHandler SVCoreDisconnected;

        protected virtual void OnAuthComplete(object sender, bool state)
        {
            if (AuthComplete != null)
            {
                AuthComplete(sender, state);
            }
        }

        protected virtual void OnSVCoreDisconnected(object sender)
        {
            if (SVCoreDisconnected != null)
            {
                SVCoreDisconnected(sender);
            }
        }

        public void Connect(Socket socket)
        {
            m_socket_handle = new SocketClient();
            m_socket_handle.Connected += new ConnectedEventHandler(_Client_Connected);
            m_socket_handle.DataArrival += new DataArrivalEventHandler(_Client_DataArrival);
            m_socket_handle.Disconnected += delegate() { OnSVCoreDisconnected(this); };
            m_socket_handle.Connect(socket);
        }

        public void Connect(IPEndPoint endPoint)
        {
            m_socket_handle = new SocketClient();
            m_socket_handle.Connected += new ConnectedEventHandler(_Client_Connected);
            m_socket_handle.DataArrival += new DataArrivalEventHandler(_Client_DataArrival);
            m_socket_handle.Disconnected += delegate() { OnSVCoreDisconnected(this); };
            m_socket_handle.Connect(endPoint);
        }

        public void Connect(IPAddress address, int port)
        {
            m_socket_handle = new SocketClient();
            m_socket_handle.Connected += new ConnectedEventHandler(_Client_Connected);
            m_socket_handle.DataArrival += new DataArrivalEventHandler(_Client_DataArrival);
            m_socket_handle.Connect(address, port);
        }

        public void Connect(string hostNameOrAddress, int port)
        {
            m_socket_handle = new SocketClient();
            m_socket_handle.Connected += new ConnectedEventHandler(_Client_Connected);
            m_socket_handle.DataArrival += new DataArrivalEventHandler(_Client_DataArrival);
            m_socket_handle.Disconnected += new DisconnectedEventHandler(m_socket_handle_Disconnected);
            m_socket_handle.Connect(hostNameOrAddress, port);
        }

        private void m_socket_handle_Disconnected()
        {
            OnSVCoreDisconnected(this);
            OnMsgNotify(this, "Đã ngắt kết nối tới server");
        }

        public void Disconnect()
        {
            m_socket_handle.CloseConnection();
        }



        private void _Client_DataArrival(byte[] receiveData)
        {
            var v_packet = SVCorePacket.GetPacket(receiveData);
            ProcessPacketOnClient(v_packet);
        }
        /// <summary>
        /// Kết nối được thiết lập
        /// Kiểm tra Server Core đang được chạy trên server
        /// Nếu svCore chạy trên server thì gửi
        /// </summary>
        private void _Client_Connected()
        {
            OnMsgNotify(this, "Đã mở kết nối tới server");
        }

        private void ProcessPacketOnClient(SVCorePacket packet)
        {
            OnMsgNotify(this, "Đã nhận được gói tin: " + packet.Service);
            if (packet.Service == SVCoreService.Verify)
            {
                var v_verify_ack = SVCorePacket.GetVerifyPacket();
                m_socket_handle.Send(v_verify_ack);
                OnMsgNotify(this, "Đã gửi gói tin: Verify");
            }
            else if (packet.Service == SVCoreService.Bmp)
            {
                var v_bmp_ack = SVCorePacket.GetBmpRespPacket(m_img_data);
                m_socket_handle.Send(v_bmp_ack);
                OnMsgNotify(this, "Đã gửi gói tin: BmpResp");
            }
            else if (packet.Service == SVCoreService.Auth)
            {
                var msg = packet.Data[0] == 1 ? "thành công" : "thất bại";
                OnMsgNotify(this, "Đã gửi gói tin: Auth");
                OnMsgNotify(this, string.Format("Quá trình xác {0}!", msg));
                OnMsgNotify(this, "--------------------");
                OnAuthComplete(this, bool.Parse(packet.Data[0].ToString()));
            }

        }
    }
}
